import requests

API_URL = "https://open.bigmodel.cn/api/paas/v4/chat/completions"  # 官方默认地址
API_KEY = "ef4c40ad6e90487c9cc38718c05e9480.UOR7nqzRAsBiBrPs"

headers = {
    "Authorization": f"Bearer {API_KEY}",
    "Content-Type": "application/json"
}

data = {
    "model": "glm-4",  # 确认是否为免费模型
    "messages": [{"role": "user", "content": "你好"}],
    "temperature": 0.7,
    "max_tokens": 200  # 限制输出长度以节省额度
}

try:
    response = requests.post(API_URL, headers=headers, json=data)
    result = response.json()
    print(result)

except Exception as e:
    print("请求失败:", str(e))